package com.dragon.sdgp.util;

import cn.hutool.core.util.RandomUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @AUTHOR : Dragon_Yang
 * @File : VerificationCodeUtil
 * @DESCRIPTION :
 * @DATE : 2023/3/12 20:05
 */
@Component
public class VerificationCodeUtil {

    @Autowired
    private RedisUtil redisUtil;

    public String getRandomCode(String to){
        String vCode = RandomUtil.randomString(4);
        redisUtil.setCacheObject("vcode-" + to, vCode, 5, TimeUnit.MINUTES);
        return vCode;
    }

    public boolean verification(String to, String vCode){
        String code = redisUtil.getCacheObject("vcode-" + to);
        if(!StringUtil.isEmpty(vCode) && vCode.equals(code)){
            return redisUtil.deleteObject("vcode-" + to);
        }
        return false;
    }
}
